package com.example.demo.phone.task;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.exceptions.ClientException;
import com.example.demo.phone.entity.PhoneDTO;
import com.example.demo.phone.mapper.PhoneMapper;
import com.example.demo.phone.util.HttpUtil;
import com.example.demo.phone.util.PhoneUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lkq
 * @date
 * @description:
 **/
@Configuration
@EnableScheduling
public class PhoneTask {

    @Autowired
    private PhoneMapper mapper;

    @Scheduled(cron = "0 */1 * * * ?")
    private void configureTasks() throws ClientException {
        String message = "";
        Integer code = null;
        List<PhoneDTO> list = mapper.list(null);
        for (PhoneDTO dto : list) {
            if (dto.getCode() == -1) {
                code = queryApple(dto.getStore(), dto.getModel());
            } else if (dto.getCode() == 0) {
                Map<String, String> map = new HashMap<String, String>();
                map.put("name", dto.getStore());
                map.put("address", dto.getModel());
                String s = PhoneUtil.sendPhone("13703910710", JSON.toJSONString(map));
                if (s != null && s.equals("OK")) {
                    code = 1;
                } else{
                    code = 0;
                }
            }
            if (code == null) {
                message = "信息获取异常！";
                continue;
            }
            dto.setCode(code);
            dto.setMessage(message);
            mapper.update(dto);
        }
    }

    public Integer queryApple(String store, String model) {

        String receive = null;
        Map<String, Object> params = new HashMap();
        String urlStr = "https://www.apple.com.cn/shop/fulfillment-messages?store=" + store + "&little=false&mt=regular&parts.0=" + model;
        try {
            receive = HttpUtil.getForString(urlStr, params);
            System.out.println("receive:" + receive);
            Map maps = (Map) JSON.parse(receive);
            maps = (Map) maps.get("body");
            maps = (Map) maps.get("content");
            maps = (Map) maps.get("pickupMessage");
            List list = (List) maps.get("stores");
            maps = (Map) list.get(0);
            maps = (Map) maps.get("partsAvailability");
            maps = (Map) maps.get(model);
            String result = (String) maps.get("pickupSearchQuote");
            if (result.equals("不可取货")) {
                return -1;
            } else {
                String storeName = (String) maps.get("storePickupQuote");
                String modelName = (String) maps.get("storePickupProductTitle");
                Map<String, String> map = new HashMap<String, String>();
                map.put("name", storeName);
                map.put("address", modelName);
                String code = PhoneUtil.sendPhone("13703910710", JSON.toJSONString(map));
                if (code != null && code.equals("OK")) {
                    return 1;
                }
                return 0;
            }
        } catch (Exception e) {
            System.out.printf(e.getMessage());
            return -2;
        }
    }
}
